package com.cqs.leetcode.math;

import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * lixw
 * 2021/11/1
 **/
public class EvaluateReversePolishNotation150 {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String t : tokens) {
            Integer v1 = 0,v2 = 0;
            if ("+".equals(t)|| "-".equals(t) || "*".equals(t)|| "/".equals(t) ) {
                v1 = stack.pop();
                v2 = stack.pop();
            }
            switch (t) {
                case "+":
                    stack.push(v1 + v2);
                    break;
                case "-":
                    stack.push(v2 - v1);
                    break;
                case "*":
                    stack.push(v1 * v2);
                    break;
                case "/":
                    stack.push(v2 / v1);
                    break;
                default:
                    stack.push(Integer.valueOf(t));
            }
        }
        return stack.pop();
    }

    public static void main(String[] args) {
        List<String[]> cases = new ArrayList<>();
        EvaluateReversePolishNotation150 e = new EvaluateReversePolishNotation150();
        cases.add(new String[]{"10","6","9","3","+","-11","*","/","*","17","+","5","+"});
        cases.add(new String[]{"2","1","+","3","*"});
        cases.add(new String[]{"4","13","5","/","+"});
        for (String[] c : cases) {
            int i = e.evalRPN(c);
            System.out.println(Lists.newArrayList(c)+"--->" + i);
        }

    }
}
